﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VINETLIFE.DO;
using VINETLIFE.BL;
using System.Web.Caching;
using System.Data;

namespace VINETLIFE.Module
{
    public partial class ucVnHomeProductsLastVersion : System.Web.UI.UserControl
    {
        private Cache cache = HttpContext.Current.Cache;
        private cmsProductBL productBL = new cmsProductBL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                rptHomeCategoriesDataSource();
            }
        }
        protected int GetLocationId()
        {
            int locationId = 0;
            if (Session["LocationId"] == null)
            {
                locationId = 0;
            }
            else
            {
                locationId = int.Parse(Session["LocationId"].ToString());
            }
            return locationId;
        }
        protected void rptHomeCategoriesDataSource()
        {
            if (cache["HomeCategories"] == null)
            {
                DataTable dtCache = new cmsProductCategoryBL().SelectHomePage(10);
                if (dtCache != null)
                    cache.Insert("HomeCategories", dtCache, null, DateTime.Now.AddSeconds(150), TimeSpan.Zero);
            }
            rptHomeCategories.DataSource = (DataTable)cache["HomeCategories"];
            rptHomeCategories.DataBind();
        }
        protected void rptHomeCategories_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            string cacheName = "";
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataRowView drv = (DataRowView)e.Item.DataItem;
                Repeater rptHomeProducts = (Repeater)e.Item.FindControl("rptHomeProducts");
                if (rptHomeProducts != null)
                {
                    int productCategoryId = int.Parse(drv["ProductCategoryId"].ToString());
                    cacheName = "HomeCategory_" + productCategoryId;
                    if (cache[cacheName] == null)
                    {
                        DataTable dtCache = productBL.SelectFrontPage(8, GetLocationId(), productCategoryId, 1, -1, -1, -1, -1);
                        if (dtCache != null)
                            cache.Insert(cacheName, dtCache, null, DateTime.Now.AddSeconds(150), TimeSpan.Zero);
                    }
                    rptHomeProducts.DataSource = (DataTable)cache[cacheName];
                    rptHomeProducts.DataBind();
                }
            }
        }
        protected void rptHomeProducts_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                DataRowView drv = (DataRowView)e.Item.DataItem;

                bool isNew = (bool)drv["IsNew"];
                bool isPromotion = (bool)drv["IsPromotion"];
                Int64 price = (Int64)drv["Price"];
                Int64 salePrice = (Int64)drv["SalePrice"];
                double percent = 0;
                try
                {
                    percent = (double)(((price - salePrice) * 100) / price);
                }
                catch { percent = 0; }

                Label lblType = (Label)e.Item.FindControl("lblType");
                Panel pnlPromotion = (Panel)e.Item.FindControl("pnlPromotion");
                Panel pnlNonPromotion = (Panel)e.Item.FindControl("pnlNonPromotion");
                Literal ltrSale = (Literal)e.Item.FindControl("ltrSale");
                pnlPromotion.Visible = false;
                pnlNonPromotion.Visible = true;
                if (isNew)
                {
                    lblType.Text = "New";
                    pnlPromotion.Visible = false;
                    pnlNonPromotion.Visible = true;
                }
                if (isPromotion)
                {
                    lblType.Text = "Sale";
                    pnlPromotion.Visible = true;
                    pnlNonPromotion.Visible = false;
                    ltrSale.Text = percent + "%";
                }
                if (isNew && isPromotion)
                {
                    lblType.Text = "Sale";
                    pnlPromotion.Visible = true;
                    pnlNonPromotion.Visible = false;
                    ltrSale.Text = percent + "%";
                }
            }
        }
    }
}